package com.baidumap.administrator.happystudy.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.ab.dialog.loadingDialog.LoadingDialogUtil;
import com.ab.http.OnMessageResponse;
import com.ab.util.AbStrUtil;
import com.ab.util.ToastUtil;
import com.ab.view.simple_rating_bar.SimpleRatingBar;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.api.Api;
import com.baidumap.administrator.happystudy.base.AppBaseActivity;
import com.baidumap.administrator.happystudy.base.Constant;
import com.baidumap.administrator.happystudy.model.OrderAffrim;
import com.baidumap.administrator.happystudy.model.OrderDetail;
import com.baidumap.administrator.happystudy.module.shopcar.MainActivity;
import com.baidumap.administrator.happystudy.utils.GlideUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

import static com.baidumap.administrator.happystudy.R.id.tv_shop_no;

/**
 * 描述：
 * 作者：xiaoxing on 17/4/18 06:40
 * 邮箱：2235445233@qq.com
 */
public class OrderDetailActivity extends AppBaseActivity implements OnMessageResponse {

    @Bind(R.id.ll_shop)
    LinearLayout ll_shop;
    @Bind(R.id.ll_product_detail)
    LinearLayout ll_product_detail;
    @Bind(R.id.img_shop)
    ImageView imgShop;
    @Bind(R.id.tv_shop_name)
    TextView tvShopName;
    @Bind(R.id.simpleRatingBar)
    SimpleRatingBar simpleRatingBar;
    @Bind(tv_shop_no)
    TextView tvShopNo;
    @Bind(R.id.tv_tel)
    TextView tvTel;
    @Bind(R.id.tv_order_no)
    TextView tvOrderNo;
    @Bind(R.id.tv_pay_time)
    TextView tvPayTime;
    @Bind(R.id.tv_num)
    TextView tvNum;
    @Bind(R.id.tv_total_money)
    TextView tvTotalMoney;
    private String mOrderId;
    private String mShppId;
    private String mShopName;


    @Override
    public int bindLayout() {
        return R.layout.activity_order_detail;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        getBundleValue();
    }

    @Override
    public void initData() {
        super.initData();
    }

    @Override
    public void doBusiness(Context mContext) {
        super.doBusiness(mContext);
        setHeaderBack();
        setHeaderTitle(R.string.order_detail);
        LoadingDialogUtil.showGifdialog(getSupportFragmentManager());
        Api.orderDetail(this, sHelper.getString(Constant.TOKEN), sHelper.getString(Constant.UID), mOrderId);
    }

    private void getBundleValue() {
        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {

            mOrderId = bundle.getString("order_id");
        }
    }


    @Override
    public void onMessageResponse(String url, JSONArray jo) throws JSONException {

    }

    @Override
    public void onMessageResponse(String url, String str) throws Exception {

    }

    @Override
    public void onMessageResponse(String url, JSONObject jo) throws JSONException {

        if (url.equals(Constant.ORDER_DETAIL) && jo != null) {
            OrderDetail orderDetail = new OrderDetail(jo.toString());

            if (orderDetail != null) {
                if (orderDetail.getCode().equals("200")) {

                    OrderDetail.DataBean dataBean = orderDetail.getData();

                    mShppId = dataBean.getShop_id();
                    mShopName = dataBean.getShop_name();

                    GlideUtil.displayImg(this, orderDetail.getData().getGoods().get(0).getProduct_image(), imgShop);
                    tvShopName.setText(dataBean.getShop_name());
                    tvShopNo.setText("商铺号：" + dataBean.getShop_id());
                    simpleRatingBar.setRating(5);
                    tvOrderNo.setText(dataBean.getOrder_no());
                    tvPayTime.setText(AbStrUtil.stampToDate(dataBean.getCreate_time()));
                    tvTel.setText(dataBean.getMobile());
                    tvNum.setText("x" + dataBean.getGoods_num());
                    tvTotalMoney.setText("￥" + dataBean.getTotal());

                    List<OrderDetail.DataBean.GoodsBean> goodsBeanList = dataBean.getGoods();

                    for (int i = 0; i < goodsBeanList.size(); i++) {
                        ll_product_detail.addView(addGoodsAttrsView(goodsBeanList.get(i)));
                    }


                } else {
                    ToastUtil.showMessage(this, orderDetail.getMsg());
                }
            }

        }
        if (url.equals(Constant.ORDER_AFFIRM) && jo != null) {
            OrderAffrim orderAffrim = new OrderAffrim(jo.toString());

            if (orderAffrim != null) {
                if (orderAffrim.getCode().equals("200")) {

                    ToastUtil.showMessage(this, "确认收货成功");

                    setResult(200);
                    finish();

                } else {
                    ToastUtil.showMessage(this, orderAffrim.getMsg());
                }
            }

        }

    }

    private View addGoodsAttrsView(OrderDetail.DataBean.GoodsBean goodsBean) {

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.include_goods_attrs, null);

        TextView tv_goods_name = (TextView) view.findViewById(R.id.tv_goods_name);
        TextView tv_goods_num = (TextView) view.findViewById(R.id.tv_goods_num);
        TextView tv_goods_money = (TextView) view.findViewById(R.id.tv_goods_money);

        tv_goods_name.setText(goodsBean.getProduct_name());
        tv_goods_num.setText("x" + goodsBean.getProduct_num());
        tv_goods_money.setText("￥" + goodsBean.getSale_price());

        view.setLayoutParams(lp);
        return view;
    }

    @OnClick({R.id.btn_shou_huo, R.id.ll_shop})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_shop:
                Bundle bundle = new Bundle();
                bundle.putString("shop_id", mShppId);
                bundle.putString("title", mShopName);
                startBundleActivity(bundle, MainActivity.class);
                break;
            case R.id.btn_shou_huo:

                LoadingDialogUtil.showGifdialog(getSupportFragmentManager());
                Api.orderAffirm(this, sHelper.getString(Constant.TOKEN), sHelper.getString(Constant.UID), mOrderId);
//                ToastUtil.showMessage(this, "接口正在开发中...");
                break;
        }
    }
}
